package com.zhongyi.http;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Timestamp;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.zhongyi.entity.Company;
import com.zhongyi.entity.Contact;
import com.zhongyi.http.base.BaseAction;
import com.zhongyi.http.service.FileService;
import com.zhongyi.util.Constant;
import com.zhongyi.util.HttpUtil;
import com.zhongyi.util.ImportUtil;
import com.zhongyi.util.StringUtil;
import com.zhongyi.util.UUIDGenerator;

/**
 * 企业信息-源文件上传
 * 
 * <p>
 * 
 *
 * @author <a href="mailto:lysongfei@gmail.com">songfei</a>
 * @version 1.0, 2012-6-9
 */
public class FileAction extends BaseAction {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private File doc;
	// 封装上传文件类型的属性
    private String docContentType;
    // 封装上传文件名的属性
    private String docFileName;
    private String savePath;
    
    private FileService fileService;
    
	public File getDoc() {
		return doc;
	}
	public void setDoc(File doc) {
		this.doc = doc;
	}
	public String getDocContentType() {
		return docContentType;
	}
	public void setDocContentType(String docContentType) {
		this.docContentType = docContentType;
	}
	public String getDocFileName() {
		return docFileName;
	}
	public void setDocFileName(String docFileName) {
		this.docFileName = docFileName;
	}
	public String getSavePath() {
		return savePath;
	}
	public void setSavePath(String savePath) {
		this.savePath = HttpUtil.getServletContext().getRealPath(savePath);
	}
	public void setFileService(FileService fileService) {
		this.fileService = fileService;
	}
	
	public String upload(){
		FileOutputStream fos = null;
        FileInputStream fis = null;
		Workbook wkbook = null;
		Sheet sheet = null;
		Row row = null;
		Company corp = null;
		Contact contact = null;
		
        try {
            // 建立文件上传流
            fis = new FileInputStream(doc);
            fos = new FileOutputStream(savePath + "\\" + getDocFileName());
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = fis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
            fos.flush();
            fis = new FileInputStream(savePath + "\\" + getDocFileName());
            wkbook = new XSSFWorkbook(fis);
            sheet = wkbook.getSheetAt(0);
			int rowCount = sheet.getLastRowNum();
			
			for (int i = 1; i <= rowCount; i++) {
				corp = new Company();
				row = sheet.getRow(i);
				//判断当前行是否为空行
				if(ImportUtil.isRowNull(row)){
					break;
				}
				String corpId = UUIDGenerator.create();
				//company message
				corp.setId(corpId);
				corp.setCorp_type(ImportUtil.getCellValue(row.getCell(1)));
				corp.setKeyword(ImportUtil.getCellValue(row.getCell(2)));
				corp.setArea_id(ImportUtil.getCellValue(row.getCell(3)));
				corp.setCorp_name(ImportUtil.getCellValue(row.getCell(4)));
				corp.setLinker(ImportUtil.getCellValue(row.getCell(5)));
				corp.setOrigin_site(ImportUtil.getCellValue(row.getCell(10)));
				corp.setCreate_time(new Timestamp(System.currentTimeMillis()));
				corp.setRemark(ImportUtil.getCellValue(row.getCell(11)));
				this.fileService.saveCorp(Constant.CORP_INSERT_SQL, corp);
				//Email
				String value = ImportUtil.getCellValue(row.getCell(6));
				String[] vs = null;
				if(value != null && value.length() > 0){
					vs = value.split("\\|");
					for(String v : vs){
						contact = new Contact();
						contact.setId(UUIDGenerator.create());
						contact.setCorp_id(corpId);
						contact.setContact_info(StringUtil.parseString(v));
						contact.setInfo_type("E");
						contact.setCreate_time(new Timestamp(System.currentTimeMillis()));
						this.fileService.saveContact(Constant.CONTACT_INSERT_SQL, contact);
					}
				}
				//phone
				value = ImportUtil.getCellValue(row.getCell(7));
				if(value != null && value.length() > 0){
					vs = value.split("\\|");
					for(String v : vs){
						contact = new Contact();
						contact.setId(UUIDGenerator.create());
						contact.setCorp_id(corpId);
						contact.setContact_info(StringUtil.parseString(v));
						contact.setInfo_type("P");
						contact.setCreate_time(new Timestamp(System.currentTimeMillis()));
						this.fileService.saveContact(Constant.CONTACT_INSERT_SQL, contact);
					}
				}
				//mobile
				value = ImportUtil.getCellValue(row.getCell(8));
				if(value != null && value.length() > 0){
					vs = value.split("\\|");
					for(String v : vs){
						contact = new Contact();
						contact.setId(UUIDGenerator.create());
						contact.setCorp_id(corpId);
						contact.setContact_info(StringUtil.parseString(v));
						contact.setInfo_type("M");
						contact.setCreate_time(new Timestamp(System.currentTimeMillis()));
						this.fileService.saveContact(Constant.CONTACT_INSERT_SQL, contact);
					}
				}
				//Fax
				value = ImportUtil.getCellValue(row.getCell(9));
				if(value != null && value.length() > 0){
					vs = value.split("\\|");
					for(String v : vs){
						contact = new Contact();
						contact.setId(UUIDGenerator.create());
						contact.setCorp_id(corpId);
						contact.setContact_info(StringUtil.parseString(v));
						contact.setInfo_type("F");
						contact.setCreate_time(new Timestamp(System.currentTimeMillis()));
						this.fileService.saveContact(Constant.CONTACT_INSERT_SQL, contact);
					}
				}
			}
        } catch (Exception e) {
            System.out.println("文件上传失败");
            e.printStackTrace();
        } finally {
           try {
				fis.close();
				fos.close();
			} catch (IOException e) {
				System.out.println("文件流关闭失败！");
				e.printStackTrace();
			}
        }
        return SUCCESS;
	}
    
}
